﻿using System;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace CokAracliKontrol.SplashScreen
{
    class SplashScreen
    {
        private static Form splashForm = null;
        private static ISplashScreen splashInterface = null;
        private static Thread splashThread = null;
        private static string tempStatus = string.Empty;

        /// <summary>

        /// Show the SplashForm

        /// </summary>

        public static void Show(Type splashFormType)
        {
            if (splashThread != null)
                return;
            if (splashFormType == null)
            {
                throw (new Exception("Splash form type is null"));
            }

            splashThread = new Thread(new ThreadStart(delegate()
            {
                CreateInstance(splashFormType);
                Application.Run(splashForm);
            }));

            splashThread.IsBackground = true;
            splashThread.SetApartmentState(ApartmentState.STA);
            splashThread.Start();
        }

        /// <summary>

        /// set the loading Status

        /// </summary>

        public static string Status
        {
            set
            {
                if (splashInterface == null || splashForm == null)
                {
                    tempStatus = value;
                    return;
                }
                splashForm.Invoke(
                        new SplashStatusChangedHandle(delegate(string str)
                        { splashInterface.SetStatusInfo(str); }),
                        new object[] { value }
                    );
            }

        }

        /// <summary>

        /// Close the SplashForm

        /// </summary>

        public static void Close()
        {
            if (splashThread == null || splashForm == null) return;

            try
            {
                splashForm.Invoke(new MethodInvoker(splashForm.Close));
            }
            catch (Exception)
            {
            }
            splashThread = null;
            splashForm = null;
        }

        private static void CreateInstance(Type FormType)
        {

            object obj = FormType.InvokeMember(null,
                                BindingFlags.DeclaredOnly |
                                BindingFlags.Public |
                BindingFlags.NonPublic |
                                BindingFlags.Instance |
                BindingFlags.CreateInstance, null, null, null);
            splashForm = obj as Form;
            splashInterface = obj as ISplashScreen;
            if (splashForm == null)
            {
                throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form"));
            }
            if (splashInterface == null)
            {
                throw (new Exception("Must implement interface ISplashScreen"));
            }

            if (!string.IsNullOrEmpty(tempStatus))
                splashInterface.SetStatusInfo(tempStatus);
        }

        private delegate void SplashStatusChangedHandle(string NewStatusInfo);
    }
}